home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / desktop / addmenu4.zip / SOURCE.ZIP / AMFILTER.C < prev    next >
C/C++ Source or Header  |  1992-04-30  |  5KB  |  189 lines

  1. /* amfilter.c
  2.  *
  3.  * This file provides the hooks (system filters) for processing of menus for AddMenu.
  4.  *
  5.  * This program is copywrite of Robert M. Ryan, 1992.  It is provided without warrantee
  6.  * of any sort.  This program is FreeWare, and can be used and distributed for
  7.  * non-commercial use without fee providing that:
  8.  *
  9.  *     a) it is not to be altered without my permission;
  10.  *    b) my name remains on the package at all times;
  11.  *    c) any programs which employ code taken from this program must credit me for
  12.  *       the appropriate routines; and
  13.  *    d) no fee is ever charged for the distribution of the program short of the cost
  14.  *       of disk media and shipping cost (if any).
  15.  *
  16.  * If you want to use it for commercial purposes or have any questions about these policies,
  17.  * do not hesitate to contact me.
  18.  *
  19.  * Robert M. Ryan, v0.1.0, 28 April 1992, first public release
  20.  *
  21.  * Rob Ryan
  22.  *    internet:   Robert_Ryan@brown.edu
  23.  *    bitnet:     ST802200@BROWNVM.BITNET
  24.  *    Compu$erve: 70324,227
  25.  */
  26.  
  27. #include <windows.h>
  28. #include "amfilter.h"
  29.  
  30. #define PROGNAME    "AMFilter"
  31. #define VERSION        PROGNAME " v0.1.0"
  32. #define VERDATE        __DATE__
  33. #define PROGRAMMER    "Robert M. Ryan"
  34. #define MESSAGE_TYPE    WH_GETMESSAGE
  35.  
  36.  
  37. /* typedefs */
  38.  
  39. /* HookData
  40.  *
  41.  * This is the data structure used by WH_CALLWNDPROC.  I've choosen element names
  42.  * that coincide with the MSG structure to minimize differences between WH_CALLWNDPROC
  43.  * filters and others.  WH_CALLWNDPROC is the only one that uses this structure.  All
  44.  * others use the MSG structure.
  45.  */
  46. typedef struct tagHookData {
  47.     WORD lParamh;
  48.     WORD lParaml;
  49.     WORD wParam;
  50.     WORD message;
  51.     WORD hwnd;
  52. } HOOKDATA;
  53. typedef HOOKDATA FAR *LPHOOKDATA;
  54.  
  55.  
  56. /* global data */
  57.  
  58. FARPROC lpOldHook, lpCWPOldHook;    /* the old hook procs */
  59. FARPROC lpHook,    lpCWPHook;        /* the new hook procs */
  60. BOOL    bInstalled;            /* are the hooks installed? */
  61. HWND    hwndHandler;            /* handle of the window processing private messages */
  62.  
  63.  
  64. /* the code */
  65.  
  66. /* LibMain
  67.  *
  68.  * The obligatory main() proc for DLLs.
  69.  */
  70. #pragma argsused
  71. int FAR PASCAL LibMain(HANDLE hInstance, WORD wDataSeg, WORD cbHeapSize, LPSTR lpCmdLine)
  72. {
  73.     UnlockData(0);
  74.     bInstalled = FALSE;
  75.     return 1;
  76. } /* LibMain */
  77.  
  78.  
  79. /* WEP
  80.  *
  81.  * The Windows Exit Procedure.  The DLL calls this before it removes itself from memory.
  82.  * It makes sense to make sure the hook is removed before removing the code for that
  83.  * hook.
  84.  */
  85. #pragma argsused
  86. int FAR PASCAL WEP(int nParameter)
  87. {
  88.     if (bInstalled)            /* remove hooks if any */
  89.     FreeHook();
  90.     return 1;
  91. } /* WEP */
  92.  
  93.  
  94. /* SetHook
  95.  *
  96.  * This returns TRUE if the hooks are set correctly, FALSE if an error occurs.  Must be
  97.  * provided a handle of a window that can understand the private messages PM_ADDTOMENU
  98.  * and PM_MENUOPTION.
  99.  */
  100. BOOL FAR PASCAL SetHook(HWND hWnd)
  101. {
  102.     HANDLE  hModule;
  103.  
  104.     if (bInstalled)
  105.     return TRUE;
  106.  
  107.     hwndHandler = hWnd;                /* set handle of private message handler */
  108.  
  109.     hModule = GetModuleHandle(PROGNAME);
  110.  
  111.     lpHook = GetProcAddress(hModule, "MessageFilter");
  112.     if (!lpHook) {
  113.     MessageBox(NULL, PROGNAME ": Error getting MessageFilter address", "Error", MB_OK | MB_ICONINFORMATION);
  114.     return FALSE;
  115.     }
  116.     lpOldHook = SetWindowsHook(MESSAGE_TYPE, lpHook);
  117.  
  118.     lpCWPHook = GetProcAddress(hModule, "CWPMessageFilter");
  119.     if (!lpCWPHook) {
  120.     MessageBox(NULL, PROGNAME ": Error getting CWPMessageFilter address", "Error", MB_OK | MB_ICONINFORMATION);
  121.     return FALSE;
  122.     }
  123.     lpCWPOldHook = SetWindowsHook(WH_CALLWNDPROC, lpCWPHook);
  124.  
  125.     bInstalled = TRUE;
  126.     return TRUE;
  127. } /* SetHook */
  128.  
  129.  
  130. /* FreeHook
  131.  *
  132.  * Unhook any windows hooks used by this program
  133.  */
  134. void FAR PASCAL FreeHook(void)
  135. {
  136.     if (!bInstalled)
  137.     return;
  138.     UnhookWindowsHook(MESSAGE_TYPE, lpHook);
  139.     UnhookWindowsHook(WH_CALLWNDPROC, lpCWPHook);
  140.     bInstalled = FALSE;
  141. } /* FreeHook */
  142.  
  143.  
  144. /* HookInstalled
  145.  *
  146.  * Returns TRUE if the hook is currently installed.  FALSE if not.
  147.  */
  148. BOOL FAR PASCAL HookInstalled(void)
  149. {
  150.     return bInstalled;
  151. } /* HookInstalled */
  152.  
  153.  
  154. /* CWPMessageFilter
  155.  *
  156.  * This is the filter function for the WH_CALLWNDPROC hook.  This function determines when a
  157.  * menu is supposed to be initialized.
  158.  *
  159.  * Sends private message to AddMenu with the hwnd of window to be initialized in wParam.
  160.  */
  161. void FAR PASCAL CWPMessageFilter(int nCode, WORD wParam, DWORD lParam)
  162. {
  163.     LPHOOKDATA    lpMsg = (LPHOOKDATA) lParam;
  164.  
  165.     if ((nCode >= 0) && (lpMsg->message == WM_INITMENU))
  166.     SendMessage(hwndHandler, PM_ADDTOMENU, lpMsg->hwnd, 0L);
  167.  
  168.     DefHookProc(nCode, wParam, lParam, &lpCWPOldHook);
  169.     return;
  170. } /* CWPMessageFilter */
  171.  
  172.  
  173. /* MessageFilter
  174.  *
  175.  * This is a hook function for WH_GETMESSAGE that traps the WM_SYSCOMMAND message.  Basically,
  176.  * this function determines when the user has selected a private message from the system menu.
  177.  *
  178.  * Sends private message to AddMenu with the id of the menu option in wParam.
  179.  */
  180. void FAR PASCAL MessageFilter(int nCode, WORD wParam, DWORD lParam)
  181. {
  182.     LPMSG    lpMsg = (LPMSG) lParam;
  183.  
  184.     if ((nCode >= 0) && (lpMsg->message == WM_SYSCOMMAND) && (lpMsg->wParam < 0xf000))
  185.     PostMessage(hwndHandler, PM_MENUOPTION, lpMsg->wParam & 0xfff0, 0L);
  186.  
  187.     DefHookProc(nCode, wParam, lParam, &lpOldHook);
  188. } /* MessageFilter */
  189.